<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .color {
            color: green;
        }

        div {
            width: 300px;
            height: 300px;
            border: 2px solid;
        }

        .bgColor{
            background-color: red;
        }
    </style>
</head>

<body>


    <div class="abc zxc dfg">Lorem ipsum dolor sit amet.</div>


    <button id="btn">切换</button>



    <script>
        let btn = document.getElementById('btn');
        //==== 操作类列表 ====
        let div = document.getElementsByTagName('div')[0];
        console.dir(div)
        console.log(div.classList);
        console.log(div.classList[1]);

        //----- 添加类  add()
        div.classList.add('color')


        //----- 删除类 remove()
        div.classList.remove('color')

        //----- 检测是否含有类 contains()
        let a = div.classList.contains('abc');
        let b = div.classList.contains('color');
        console.log(a, b);

        //----- 切换类 toggle()
        let i = 0;
        btn.onclick = function () {
            console.log(`你点击了${i + 1}次`);
            div.classList.toggle('bgColor');
            // div.classList.contains('bgColor')?div.classList.remove('bgColor'):div.classList.add('bgColor');
            i++

        }






    </script>
</body>

</html>